using UnityEngine;
using UnityEngine.UI;
using TEngine;
using System.Collections.Generic;
using GameLogic.Localization;
using System;
using TMPro;
using GameProtocol;
using GameConfig.ships;
using UnityEngine.EventSystems;

namespace GameLogic
{
	partial class ShipBluemapPanel
    {
        Dictionary<int, SlotInBluemapUI> m_SlotUI = new Dictionary<int, SlotInBluemapUI>();


        Action<int> m_OnSlotSelectedCallback;
        int m_CurrentSelectedSlotID = -1;

        public Action<int> OnSlotSelectedCallback { get => m_OnSlotSelectedCallback; set => m_OnSlotSelectedCallback = value; }
        #region 生命周期
        protected override void OnCreate()
        {
            base.OnCreate();
            m_imgPreview.gameObject.SetActive(false);   
        }
        protected override void RegisterEvent()
        {
            base.RegisterEvent();
            var trigger = gameObject.transform.FindChildComponent<EventTriggerEx>("Root/m_tfBluemapScrollView");
            trigger.OnScrollCallback.AddListener(OnScroll); 
            trigger.OnDragCallback.AddListener(OnDrag);
            trigger.OnDragEndCallback.AddListener(OnDragEnd);
        }
        protected override void OnRefresh()
        {
            base.OnRefresh();
        }
        #endregion
        #region 
        ShipData m_ShipData;
        public void Refresh(ShipData shipData)
        {
            m_ShipData = shipData;
            m_imgPreview.gameObject.SetActive(true);   
            foreach(var i in m_SlotUI)
            {
                PoolManager.Instance.PushGameObject(i.Value.gameObject);
            }
            m_SlotUI.Clear();

            var shipDesignData = ShipModule.Instance.GetShipDesign(shipData.CfgID);
            m_imgPreview.sprite = ShipModule.Instance.GetShipSprite(shipData.CfgID);
            foreach(var i in shipDesignData.SlotDesignDatas)
            {
                var slotDesignData = i.Value;
                var slotUIObj = PoolManager.Instance.GetGameObject("Assets/AssetRaw/UI/ShipConfigUI/SlotInBluemapWidget.prefab");
                var slotUI = slotUIObj.GetComponent<SlotInBluemapUI>();
                slotUI.transform.SetParent(m_tfSlots);
                slotUI.OnSelectedCallback = OnSlotSelected;
                slotUI.Init(slotDesignData, i.Key);
                if(shipData.SlotEquipment.ContainsKey(i.Key))
                    slotUI.SwitchWeapon(shipData.SlotEquipment[i.Key].EquipmentID);
                m_SlotUI.Add(i.Key, slotUI);
            }
        }
        public void OnWeaponChanged(int weaponID)
        {
            m_SlotUI?[m_CurrentSelectedSlotID].SwitchWeapon(weaponID);
        }
        #endregion

        #region 私有

        void OnDrag(PointerEventData eventData)
        {
            //m_tfContent.GetComponent<RectTransform>().anchoredPosition += eventData.delta;
        }
        void OnDragEnd(PointerEventData eventData)
        {

        }
        void OnScroll(PointerEventData eventData)
        {
            var scale = m_tfContent.localScale.x;
            scale = Mathf.Clamp(scale + eventData.scrollDelta.y * Time.deltaTime * 20, 0.2f, 5f);
            m_tfContent.localScale = new Vector3(scale, scale, scale);
        }
        void OnSlotSelected(int slotID)
        {
            m_CurrentSelectedSlotID = slotID;
            if(m_OnSlotSelectedCallback != null)
                m_OnSlotSelectedCallback.Invoke(m_CurrentSelectedSlotID);

            var designID = ShipModule.Instance.GetShipDesign(m_ShipData.CfgID);
            m_cChangerHighlight.ChangeToIndex(designID.SlotDesignDatas[slotID].SlotSize);
            m_cChangerHighlight.gameObject.SetActive(true);
            m_cChangerHighlight.transform.SetParent(m_SlotUI[slotID].transform);
            m_cChangerHighlight.transform.localPosition = Vector3.zero;
            m_cChangerHighlight.transform.localScale= Vector3.one;
            m_cChangerHighlight.GetComponent<RectTransform>().sizeDelta = m_SlotUI[slotID].GetComponent<RectTransform>().sizeDelta;

        }
        #endregion
    }
}